package cn.lifeaid.shiro;

import java.util.List;
import java.util.Set;

import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.lifeaid.db.SysResource;
import cn.lifeaid.service.SysResourceService;
import cn.lifeaid.service.SysUserService;
import cn.lifeaid.utils.Constants;

/**
 * 初始化系统数据
 * 
 * @author shi_senlin
 *
 */
@Service
public class InitSystemData {
	@Autowired
	private SysUserService userService;
	@Autowired
	private SysResourceService resourceService;

	/**
	 * 根据当前登录的用户，加载对应的右侧菜单栏信息放到session中
	 * 
	 * @param subject
	 */
	public void initMenu(Subject subject) {
		Session session = subject.getSession();
		if (null == session.getAttribute(Constants.SESSION_MENUS)) {
			Set<String> permissions = userService.findPermissions(subject
					.getPrincipal().toString());
			List<SysResource> menus = resourceService.findMenus(permissions);
			session.setAttribute(Constants.SESSION_MENUS, menus);
		}
	}
}
